<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Department */
/* @var $form yii\widgets\ActiveForm */
?>
<style type="text/css">
    .checkbox_list {
        background-color: #eee;
        padding: 0.2em;
    }

    .checkbox_list .labels label {
        padding: 8px 3px 8px 8px;
        width: 180px;
    }
</style>

<div class="department-form">

    <?php $form = ActiveForm::begin(['options' => ['id' => 'disease_form', 'name' => 'disease_form']]); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <div class="checkbox_list">
        <?= $form->field($model, 'disease_ids')->hiddenInput()->label('属于科室的病种') ?>
        <div class="labels">
            <table style="width:100%;border-top:1px #ccc solid;border-left:1px #ccc solid;">
                <?php foreach ($disease_array as $root) { ?>
                    <tr style="background-color: #fff;border-bottom:1px #ccc solid;">
                        <td style="border-right: #ccc 1px solid;" width="200">
                            <label><input type="checkbox" <?php echo $root['checked']?'checked="checked"':'' ?> onchange="disease_root_select(this)" name="disease_id_array[]" value="<?= $root['id'] ?>"> <?= $root['name'] ?></label>
                        </td>
                        <td style="border-right: #ccc 1px solid;" width="600" id="disease_root_<?= $root['id'] ?>_children">
                            <?php if (null != $root['children']) {
                                foreach ($root['children'] as $disease) { ?>
                                    <label><input type="checkbox" <?php echo $disease['checked']?'checked="checked"':'' ?> name="disease_id_array[]" value="<?= $disease['id'] ?>"> <?= $disease['name'] ?></label>
                            <?php }
                            } ?>
                        </td>
                    </tr>
                <?php } ?>
            </table>
        </div>
    </div>

    <br />
    <div class="checkbox_list">
        <?= $form->field($model, 'doctor_ids')->hiddenInput()->label('科室在职的医生') ?>
        <div class="labels" style="background-color: #fff;">
            <?php foreach ($doctor_array as $d) { ?>
                <label><input type="checkbox" <?php echo $d['checked']?'checked="checked"':'' ?> name="doctor_id_array[]" value="<?= $d['id'] ?>"> <?= $d['name'] ?></label>
            <?php } ?>
        </div>
    </div>

    <br />
    <?= $form->field($model, 'sort')->input('number') ?>

    <?= $form->field($model, 'status')->dropDownList(['显示' => '显示', '隐藏' => '隐藏',], ['prompt' => '']) ?>

    <div class="form-group">
        <?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
    function disease_root_select(obj) {
        if(obj.checked){
            $('#disease_root_' + obj.value + '_children input[type="checkbox"]').attr('checked', obj.checked);
        } else {
            $('#disease_root_' + obj.value + '_children input[type="checkbox"]').removeAttr('checked');
        }
    }

    function form_submit_before_timeout(){
        $("#disease_form").on("beforeSubmit", function(event) {
            var disease_ids_array = [];
            $('#disease_form input[name="disease_id_array[]"]').each(function(index, obj){
                if(obj.checked){
                    disease_ids_array.push(obj.value);
                }
            });
            var doctor_ids_array = [];
            $('#disease_form input[name="doctor_id_array[]"]').each(function(index, obj){
                if(obj.checked){
                    doctor_ids_array.push(obj.value);
                }
            });
            $('#department-disease_ids').val(disease_ids_array.join(','));
            $('#department-doctor_ids').val(doctor_ids_array.join(','));
            return true;
        });
    }

    window.setTimeout(form_submit_before_timeout, 1000);
</script>